package com.fdb.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.fdb.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.console.service.facade.SAccoountOrgService;
import com.fdb.efp.console.service.vo.SAccoountOrgVO;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.basic.framework.core.constant.ApplicationConstance;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class SAccoountOrgController extends AbstractController  {
	
	@Autowired
	@Qualifier("sAccoountOrgService")
	private SAccoountOrgService sAccoountOrgService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/accoount/orgs", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SAccoountOrgVO>> querySAccoountOrgAll(
			@RequestBody SAccoountOrgVO vo) {		
		// 根据用户角色获取用户权限方案
		List<SAccoountOrgVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sAccoountOrgService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = sAccoountOrgService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sAccoountOrgService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = sAccoountOrgService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/accoount/org/{orgCode}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SAccoountOrgVO> queryByPk(@PathVariable("orgCode") String orgCode) {		
		SAccoountOrgVO inVo = new SAccoountOrgVO();
				inVo.setOrgCode(orgCode);
		SAccoountOrgVO outputVo = sAccoountOrgService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param sAccoountOrg
	 * @return
	 */
	@RequestMapping(value="/s/accoount/org", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SAccoountOrgVO inSAccoountOrgVo) {
		int outputVo = sAccoountOrgService.deleteByPk(inSAccoountOrgVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSAccoountOrgVo
	 * @return
	 */
	@RequestMapping(value="/s/accoount/org", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SAccoountOrgVO inSAccoountOrgVo) {
		setUserInfoToVO(inSAccoountOrgVo);
		int outputVo = sAccoountOrgService.updateByPk(inSAccoountOrgVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inSAccoountOrgVo
	 * @return
	 */
	@RequestMapping(value="/s/accoount/org", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSAccoountOrg(@RequestBody SAccoountOrgVO inSAccoountOrgVo) {
		setUserInfoToVO(inSAccoountOrgVo);
		int outputVo = sAccoountOrgService.insertSAccoountOrg(inSAccoountOrgVo);
		if ( outputVo == -2) {
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setRows(outputVo);
			responseData.setMessage("机构代码已存在,新增失败!");
			responseData.setCode(ApplicationConstance.FAILURE);
			responseData.setSuccess(false);
			return responseData;
		}else
			return getResponseData(outputVo);
		
	}
}
